/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.ts.index;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import edu.yonsei.iwb.di.ESService;
import edu.yonsei.iwb.ts.model.JenaModel;
import edu.yonsei.iwb.ts.model.JenaModelType;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.log4j.Logger;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

/**
 *
 * @author JDK
 */
public class TSIndexerHandler extends Thread {

    private TransportClient client;
    private ArrayList<String> indexnameList;
    private ArrayList<String> filenameList;
    private boolean flag = true;
    private Logger logger = Logger.getLogger(TSIndexerHandler.class);
    private UnIndexedResourceList uirl;

    public TSIndexerHandler(ArrayList<String> indexnameList, ArrayList<String> filenameList) {

        this.indexnameList = indexnameList;
        this.filenameList = filenameList;
        this.uirl = new UnIndexedResourceList();
        this.client = new TransportClient(ImmutableSettings.settingsBuilder()
                .put("cluster.name", ESService.CLUSTER_NAME)
                .put("client.transport.sniff", false)
                .put("transport.tcp.compress", true)
                .put("transport.tcp.connect_timeout", "10s")
                .put("network.tcp.keep_alive", true));
        this.addIndexAddress(this.client);
        this.client.admin().cluster().health(new ClusterHealthRequest("_all").waitForYellowStatus().timeout("3s")).actionGet();
    }

    @Override
    public void run() {
        for (int i = 0; i < filenameList.size(); i++) {
            String indexname = indexnameList.get(i);
            String filename = filenameList.get(i);
            
            File file = new File(filename);

            flag = true;
            while (flag) {
                startIndexing(indexname, file);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }

        }
    }

//    public void stopIndexer(String filename) {
//        TSIndexer dbi = this.workingThread.get(filename);
//        dbi.stopIndexing();
//        removeFileList(filename);
//    }
    
    private void startIndexingTemp(String indexname, File file) {
        String filename = file.getPath();
        StmtIterator stit = this.getTripleList(filename);
        try{
        String conffile = "./resources/experiment2/poker/pair_owl.conf";
//        String conffile = "./resources/experiment2/poker/pair_owl.conf";
//        String conffile = "./resources/experiment2/poker/pair_owl.conf";
//        String conffile = "./resources/experiment2/poker/pair_owl.conf";
        BufferedReader br = new BufferedReader(new FileReader(new File(conffile)));
        String line = null;
        while((line = br.readLine()) != null){
            
        }
        }catch(IOException ex){
            
        }

        TSIndexer dbi = new TSIndexer(this.client, this, indexname, file.getName(), stit);
        dbi.start();
        flag = false;

    }
    
    private void startIndexing(String indexname, File file) {
        String filename = file.getPath();
        StmtIterator stit = this.getTripleList(filename);
        TSIndexer dbi = new TSIndexer(this.client, this, indexname, file.getName(), stit);
        dbi.start();
        flag = false;

    }

    private StmtIterator getTripleList(String filename) {        
        JenaModel jm = new JenaModel(JenaModelType.MEMORY_MODEL);
        jm.loadOntology(filename);
        Model model = jm.getJenaModel();
        return model.listStatements();
    }

    private void addIndexAddress(TransportClient client) {
        String discoveryList = ESService.CONF.getProperty("index.node_list");
        try {
            String[] hosts = discoveryList.split(",");
            for (int i = 0; i < hosts.length; i++) {
                String[] host = hosts[i].trim().split(":");
                String ip = host[0];
                int port = Integer.parseInt(host[1]);
                client.addTransportAddress(new InetSocketTransportAddress(ip, port));
            }
        } catch (NullPointerException npe) {
            npe.printStackTrace();
            client.addTransportAddress(new InetSocketTransportAddress(ESService.HOST, ESService.PORT));
        }
    }
}
